package com.turing.post.thirdinterface.impl;

import java.io.Serializable;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import com.turing.post.thirdinterface.util.ThirdInterfaceApplicationException;
import com.turing.post.thirdinterface.util.ThirdInterfaceRespCode;
import com.turing.post.tuxedo.util.ParaseException;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 请求银行基类
 * @author ZXL
 *
 */
@SuppressWarnings("serial")
public class PSBCBankResponse implements Serializable {
	/**
	 * 交易码
	 */
	public String tradeId = "";
	/**
	 * 发起交易的具体人或设备
	 */
	public String teller = "";

	/**
	 * 委托代码
	 */
	public String enterprise = "";

	/**
	 * 主账号
	 */
	public String custId = "";

	/**
	 * 特殊卡类型
	 */
	public String dptId = "";

	/**
	 * 客户姓名
	 */
	public String custName = "";

	/**
	 * 身份证号
	 */
	public String passId = "";

	/**
	 * 证件类型
	 */
	public String payerType = "";

	/**
	 * 卡/帐户类型
	 */
	public String accType = "";
	
	/**
	 * 卡/帐户号码
	 */
	public String acc = "";
	
	/**
	 * 交易流水号
	 */
	public String tradeSeq = "";
	
	/**
	 * 交易金额
	 */
	public String moneyTrade = "";
	/**
	 * 日期
	 */
	public String tradeDate = "";
	/**
	 * 对账文件名
	 */
	public String fileName = "";
	/**
	 * 订单号
	 */
	public String coNum = "";
	/**
	 * 原交易流水号
	 */
	public String tradeSeqOrg = "";
	/**
	 * 原交易码
	 */
	public String tradeIdOrg = "";
	/**
	 * 原交易日期
	 */
	public String tradeDateOrg = "";
	
	/**
	 * 构造开卡(销户)XML请求报文
	 * 
	 * @param doc
	 */
	public static byte[] constructOpenOrCancelCardXml(PSBCBankResponse bankresponse) throws PCPSApplicationException {
		
		Element root = new Element(PSBCBankXmlHelper.XML_NODE_BANKINTF); 
		Element pub = new Element(PSBCBankXmlHelper.XML_NODE_PUB);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_TRADEID,
				bankresponse.tradeId);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_TELLER,
				bankresponse.teller);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_ENTERPRISE,
				bankresponse.enterprise);
		root.addContent(pub);
		Element in = new Element(PSBCBankXmlHelper.XML_NODE_IN);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_CUSTID,
				bankresponse.custId);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_DPTID,
				bankresponse.dptId);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_CUSTNAME,
				bankresponse.custName);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_PASSID,
				bankresponse.passId);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_PAYERTYPE,
				bankresponse.payerType);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_ACCTYPE,
				bankresponse.accType);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_ACC,
				bankresponse.acc);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_TRADESEQ,
				bankresponse.tradeSeq);
		root.addContent(in);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getCompactFormat().setEncoding(
				PSBCBankXmlHelper.XML_CHARSET_UTF));
		try {
			return out.outputString(doc).trim().getBytes(
					PSBCBankXmlHelper.XML_CHARSET_GBK);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
	
	
	/**
	 * 构造充值(赎回)XML请求报文
	 * 
	 * @param doc
	 */
	public static byte[] constructDepositOrRedempCardXml(PSBCBankResponse bankresponse) throws PCPSApplicationException {
		
		Element root = new Element(PSBCBankXmlHelper.XML_NODE_BANKINTF);
		Element pub = new Element(PSBCBankXmlHelper.XML_NODE_PUB);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_TRADEID,
				bankresponse.tradeId);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_TELLER,
				bankresponse.teller);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_ENTERPRISE,
				bankresponse.enterprise);
		root.addContent(pub);
		Element in = new Element(PSBCBankXmlHelper.XML_NODE_IN);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_CUSTID,
				bankresponse.custId);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_DPTID,
				bankresponse.dptId);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_MONEYTRADE,
				bankresponse.moneyTrade);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_TRADESEQ,
				bankresponse.tradeSeq);
		PSBCBankXmlHelper.addStringElement(in, PSBCBankXmlHelper.XML_CO_NUM, 
				bankresponse.coNum);
		root.addContent(in);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getCompactFormat().setEncoding(
				PSBCBankXmlHelper.XML_CHARSET_UTF));
		try {
			return out.outputString(doc).trim().getBytes(
					PSBCBankXmlHelper.XML_CHARSET_GBK);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
	
	/**
	 * 构造对明细账(对总账)XML请求报文
	 * 
	 * @param doc
	 */
	public static byte[] constructReconciliationDetailOrTotalXml(PSBCBankResponse bankresponse) throws PCPSApplicationException {
		
		Element root = new Element(PSBCBankXmlHelper.XML_NODE_BANKINTF);
		Element pub = new Element(PSBCBankXmlHelper.XML_NODE_PUB);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_TRADEID,
				bankresponse.tradeId);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_TELLER,
				bankresponse.teller);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_ENTERPRISE,
				bankresponse.enterprise);
		root.addContent(pub);
		Element in = new Element(PSBCBankXmlHelper.XML_NODE_IN);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_TRADEDATE,
				bankresponse.tradeDate);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_NODE_FILENAME,
				bankresponse.fileName);
		root.addContent(in);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getCompactFormat().setEncoding(
				PSBCBankXmlHelper.XML_CHARSET_UTF));
		try {
			return out.outputString(doc).trim().getBytes(
					PSBCBankXmlHelper.XML_CHARSET_GBK);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
	
	/**
	 * 构造校正交易XML请求报文
	 * 
	 * @param doc
	 */
	public static byte[] constructAdjustBusinessXml(PSBCBankResponse bankresponse) throws PCPSApplicationException {
		
		Element root = new Element(PSBCBankXmlHelper.XML_NODE_BANKINTF);
		Element pub = new Element(PSBCBankXmlHelper.XML_NODE_PUB);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_TRADEID,
				bankresponse.tradeId);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_TELLER,
				bankresponse.teller);
		PSBCBankXmlHelper.addStringElement(pub,PSBCBankXmlHelper.XML_NODE_ENTERPRISE,
				bankresponse.enterprise);
		root.addContent(pub);
		Element in = new Element(PSBCBankXmlHelper.XML_NODE_IN);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_TRADE_SEQ_ORG,
				bankresponse.tradeSeqOrg);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_TRADE_ID_ORG,
				bankresponse.tradeIdOrg);
		PSBCBankXmlHelper.addStringElement(in,PSBCBankXmlHelper.XML_TRADE_DATE_ORG,
				bankresponse.tradeDateOrg);
		root.addContent(in);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getCompactFormat().setEncoding(
				PSBCBankXmlHelper.XML_CHARSET_UTF));
		try {
			return out.outputString(doc).trim().getBytes(
					PSBCBankXmlHelper.XML_CHARSET_GBK);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
	
	public static byte[] checkResponseBusinessType(PSBCBankResponse bankresponse) throws PCPSApplicationException {
		//验证参数
		if (bankresponse == null){
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRMSG_NULLPOINTER,
					ThirdInterfaceRespCode.RC_ARGS_NULL, "参数为空!");
		}
		//验证业务类型
		if (PCPSUtil.isEmpty(bankresponse.tradeId)){
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRMSG_NULLPOINTER,
					ThirdInterfaceRespCode.RC_ARGS_NULL, "交易码为空!");
		}
		if (PSBCBankXmlHelper.XML_OPENCARD.equals(bankresponse.tradeId) 
				|| PSBCBankXmlHelper.XML_CANCELCARD.equals(bankresponse.tradeId)){
			return constructOpenOrCancelCardXml(bankresponse);
		}
		if (PSBCBankXmlHelper.XML_DEPOSIT.equals(bankresponse.tradeId) 
				|| PSBCBankXmlHelper.XML_REDEMP.equals(bankresponse.tradeId)){
			return constructDepositOrRedempCardXml(bankresponse);
		}
		if (PSBCBankXmlHelper.XML_RECONCILIATIONDETAIL.equals(bankresponse.tradeId) 
				|| PSBCBankXmlHelper.XML_RECONCILIATIONTOTAL.equals(bankresponse.tradeId)){
			return constructReconciliationDetailOrTotalXml(bankresponse);
		}
		if (PSBCBankXmlHelper.XML_ADJUSTBUSINESS.equals(bankresponse.tradeId)){
			return constructAdjustBusinessXml(bankresponse);
		}
		return null;
	}
}
